package com.ls.lserp.modules.sys.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ls.lserp.common.mapper.JsonMapper;
import com.ls.lserp.common.utils.CacheUtils;
import com.ls.lserp.common.utils.Constants;
import com.ls.lserp.common.utils.SpringContextHolder;
import com.ls.lserp.common.utils.StringUtils;
import com.ls.lserp.modules.sys.dao.DesignoptionDao;
import com.ls.lserp.modules.sys.entity.DesignoptionCache;

import java.util.List;
import java.util.Map;

/**
 * 设计项目工具类
 *
 * @author 于辉
 * @version 2017-06-16
 */
public class DesignoptionUtils {

	private static DesignoptionDao designoptionDao = SpringContextHolder.getBean(DesignoptionDao.class);

	public static final String CACHE_DESIGNOPTION_MAP = "designoptionMap";

	public static final String CACHE_DESIGNOPTION_VALUE_MAP = "designoptionValueMap";

	/**
	 * @Title: putDesignoptionCache
	 * @Description: 服装款式设计项目全量放入缓存
	 * @author 于辉
	 *
	 */
	public static void putDesignoptionCache() {
		@SuppressWarnings("unchecked")
		Map<String, List<DesignoptionCache>> designoptionMap = (Map<String, List<DesignoptionCache>>) CacheUtils.get(CACHE_DESIGNOPTION_MAP);
		if (designoptionMap == null) {
			designoptionMap = Maps.newHashMap();
			for (DesignoptionCache designoption : designoptionDao.findAllList(new DesignoptionCache())) {
				List<DesignoptionCache> dictList = designoptionMap.get(designoption.getItemCode ());
				if (dictList != null) {
					dictList.add(designoption);
					designoptionMap.put(designoption.getItemCode(), Lists.newArrayList(dictList));
				} else {
					designoptionMap.put(designoption.getItemCode(), Lists.newArrayList(designoption));
				}

			}
			CacheUtils.put(CACHE_DESIGNOPTION_MAP, designoptionMap);
		}
	}

	/**
	 * @Title: getDesignoptionList
	 * @Description: 返回服装款式设计项目列表
	 * @author 于辉
	 *
	 * @param code
	 * @return List<Designoption>
	 *
	 */

	public static List<DesignoptionCache> getDesignoptionList(String code) {

		@SuppressWarnings("unchecked")
		Map<String, List<DesignoptionCache>> designoptionMap = (Map<String, List<DesignoptionCache>>) CacheUtils.get(CACHE_DESIGNOPTION_MAP);
		if (designoptionMap == null) {
			designoptionMap = Maps.newHashMap();
			for (DesignoptionCache designoptionCache : designoptionDao.findAllList(new DesignoptionCache())) {
				List<DesignoptionCache> dictList = designoptionMap.get(designoptionCache.getItemCode());
				if (dictList != null) {
					dictList.add(designoptionCache);
				} else {
					designoptionMap.put(designoptionCache.getItemCode(), Lists.newArrayList(designoptionCache));
				}
			}
			CacheUtils.put(CACHE_DESIGNOPTION_MAP, designoptionMap);
		}
		List<DesignoptionCache> dictList = designoptionMap.get(code);

		if (dictList == null) {
			dictList = Lists.newArrayList();
		} else {
			for (DesignoptionCache dict : dictList) {
				if (Constants.EN_US.equals(StringUtils.getLanguage())) {
					dict.setItemName(dict.getItemNameEn());
				} else {
					dict.setItemName(dict.getItemNameCn());
				}
			}
		}

		return dictList;
	}

	/**
	 * @Title: getDesignoptionEntity @Description: 款式设计项目Entity
	 * @author 于辉
	 *
	 * @param value
	 * @return Designoption
	 *
	 * @return List<Designoption>
	 */

	public static DesignoptionCache getDesignoptionEntity(String value) {

		Map<String, DesignoptionCache> dictValueMap = (Map<String, DesignoptionCache>) CacheUtils.get(CACHE_DESIGNOPTION_VALUE_MAP);

		if (dictValueMap == null) {

			dictValueMap = Maps.newHashMap();
			for (DesignoptionCache designoption : designoptionDao.findAllList(new DesignoptionCache())) {
				dictValueMap.put(designoption.getItemValue(), designoption);
			}
			CacheUtils.put(CACHE_DESIGNOPTION_VALUE_MAP, dictValueMap);

		}

		DesignoptionCache returnDesignoption = dictValueMap.get(value);

		if (returnDesignoption != null) {
			return returnDesignoption;
		} else {
			return new DesignoptionCache();
		}

	}

	/**
	 * 返回款式设计项目名称
	 *
	 * @param value
	 * @return
	 */
	public static String getDesignoptionName(String value) {

		Map<String, DesignoptionCache> dictValueMap = (Map<String, DesignoptionCache>) CacheUtils.get(CACHE_DESIGNOPTION_VALUE_MAP);

		if (dictValueMap == null) {

			dictValueMap = Maps.newHashMap();
			for (DesignoptionCache designoption : designoptionDao.findAllList(new DesignoptionCache())) {
				dictValueMap.put(designoption.getItemValue(), designoption);
			}
			CacheUtils.put(CACHE_DESIGNOPTION_VALUE_MAP, dictValueMap);

		}

		DesignoptionCache returnDesignoption = dictValueMap.get(value);

		if (returnDesignoption != null) {
			if (Constants.EN_US.equals(StringUtils.getLanguage())) {
				return returnDesignoption.getItemNameEn();
			} else {
				return returnDesignoption.getItemNameCn();
			}
		} else {
			return "";
		}
	}
	
	/**
	 * 返回款式设计项目名称
	 *
	 * @param value
	 * @return
	 */
	public static String getDesignoptionNameEn(String value) {

		Map<String, DesignoptionCache> dictValueMap = (Map<String, DesignoptionCache>) CacheUtils.get(CACHE_DESIGNOPTION_VALUE_MAP);

		if (dictValueMap == null) {

			dictValueMap = Maps.newHashMap();
			for (DesignoptionCache designoption : designoptionDao.findAllList(new DesignoptionCache())) {
				dictValueMap.put(designoption.getItemValue(), designoption);
			}
			CacheUtils.put(CACHE_DESIGNOPTION_VALUE_MAP, dictValueMap);

		}

		DesignoptionCache returnDesignoption = dictValueMap.get(value);

		if (returnDesignoption != null) {
			return returnDesignoption.getItemNameEn();
		} else {
			return "";
		}
	}
	
	/**
	 * 返回款式设计项目备注
	 *
	 * @param value
	 * @return
	 */
	public static String getDesignoptionRemark(String value) {

		Map<String, DesignoptionCache> dictValueMap = (Map<String, DesignoptionCache>) CacheUtils.get(CACHE_DESIGNOPTION_VALUE_MAP);

		if (dictValueMap == null) {

			dictValueMap = Maps.newHashMap();
			for (DesignoptionCache designoption : designoptionDao.findAllList(new DesignoptionCache())) {
				dictValueMap.put(designoption.getItemValue(), designoption);
			}
			CacheUtils.put(CACHE_DESIGNOPTION_VALUE_MAP, dictValueMap);

		}

		DesignoptionCache returnDesignoption = dictValueMap.get(value);

		if (returnDesignoption != null) {
				return returnDesignoption.getRemarks();
		} else {
			return "";
		}
	}
	
	/**
	 * @Title: getDesignoptionListJson @Description: 返回服装款式设计项目列表（JSON）
	 * @author 于辉
	 *
	 * @param code @return
	 *
	 * @return String @throws
	 */
	public static String getDesignoptionListJson(String code) {
		return JsonMapper.toJsonString(getDesignoptionList(code));
	}

}
